import { Destroyable, react, reactJson, Event, track, bind, HasOwner, createNextAnimateFrameEvent } from "xbsj-xe2/dist-node/xe2-base-utils";
import { ObjResettingWithEvent, SceneObject, Viewer } from 'xbsj-xe2/dist-node/xe2-base-objects';
import { getUrlParam } from "../utils"
import { MyProjectManager } from "../MyProjectManager";
import { ElMessage } from "element-plus";
import { ES3DTileset, ESGeoPolygon } from "esobjs-xe2-plugin/dist-node/esobjs-xe2-plugin-main";

import { CzmPolygonPrimitive } from "xbsj-xe2/dist-node/xe2-cesium-objects";
import { ModelserManager } from ".";



export class EditPolygon extends HasOwner<MyProjectManager> {

    // 获取 ESGeoExtrudedPolygonUFMS 最低点高度
    getMinHeight(points: [number, number, number][] | undefined) {
        // let points = this.getPoints(type);
        if (!points?.length) return 0;
        let a: any = [];
        for (let i = 0; i < points.length; i++) {
            const el = points[i];
            a.push([el[2] || 0]);
        }
        const min = +Math.min(...a);
        return min;
    }
    // 获取 ESGeoExtrudedPolygonUFMS 最高点高度
    getMaxHeight(points: [number, number, number][] | undefined) {
        // let points = this.getPoints(type);
        if (!points?.length) return 0;
        let a: any = [];
        for (let i = 0; i < points.length; i++) {
            const el = points[i];
            a.push([el[2] || 0]);
        }
        const min = +Math.max(...a);
        return min;
    }

    //经纬度 转 Geojson
    ArrayAdd(arr: Iterable<unknown> | null | undefined) {
        let a = [...new Set(arr)]; //去重加首尾相同
        a.push(a[0]);
        return [...a];
    }

    getGeojson(GeoJsonArr: [number, number, number][] | undefined) {
        // let GeoJsonArr = this.getPoints(type);
        if (!GeoJsonArr) return "";
        if (GeoJsonArr.length >= 3) {
            let polylineData = [];
            for (let index = 0; index < GeoJsonArr.length; index++) {
                let el: any = GeoJsonArr[index];
                if (!(el[2] instanceof Number)) {
                    el[2] = 0;
                }
                // @ts-ignore
                polylineData.push([el[0], el[1], el[2]]);
            }
            let arr = this.ArrayAdd(polylineData);
            const geojson = JSON.stringify({
                type: "MultiPolygon",
                coordinates: [[arr]],
            });
            return geojson;
        } else {
            return "";
        }
    };

    constructor(owner: MyProjectManager, private _modelserManager: ModelserManager) {
        super(owner);
    }

}